<script setup name="Reim">
	import {ref, reactive} from 'vue'
	import {reimApi} from '@/api/reim.js'
	import {
		Delete,
		Search, CloseBold
	} from '@element-plus/icons-vue'
	import {ElMessage, ElMessageBox} from "element-plus";
	import {useUserInfoStore} from "@/store/userInfo.js";
	import {deptApi} from "@/api/dept.js";

	//获取登录人员信息
	const userInfoStore = useUserInfoStore();
	const userLogin = ref({})
	userLogin.value = userInfoStore.getUserInfo()
	const loginUser = userInfoStore.getUserInfo().role
	
	//表格数据
	let reimList = ref([])
	const total = ref(0)
	
	//分页信息，搜索条件
	const reimQuery = reactive({
		page: 1,
		limit: 10,
		explain: '',
		name: ''
	})
	
	const loadData = () => {
		reimApi.list(reimQuery).then(result => {
			reimList.value = result.data.list
			total.value = result.data.total
		})
	}
	
	loadData()
	
	const onSearch = () => {
		reimQuery.page = 1
		loadData()
	}
	
	//条件搜索——部门下拉框
	const options = ref([])
	//获取部门数据
	const selectAllDept = () => {
		deptApi.selectAll().then(result => {
			options.value = result.data
		})
	}
	selectAllDept()
	
	//审核管理员数据
	import {adminApi} from "@/api/admin.js";
	
	const aOptions = ref([])
	//获取审核人数据
	const selectAllAdmin = () => {
		aOptions.value=[]
		adminApi.selectAllAdmin().then(result => {
			aOptions.value = result.data
		})
	}
	selectAllAdmin()
	
	const selectByDId = (value) => {
		aOptions.value=[]
		if(value){
			adminApi.selectByDId(value).then(result => {
				aOptions.value = result.data
			})
		}
	}
	
	//单个删除
	const deleteById = (id) => {
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			.then(() => {
				reimApi.deleteById(id).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
				
			})
	}
	
	//批量删除
	const ids = ref([])
	const handleSelectionChange = (rows) => {
		ids.value = rows.map(row => row.id)
		console.log(ids.value);
	}
	const deleteAll = () => {
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll: false   //锁定滚动条。防止抖动
			}
		)
			//点击确认删除
			.then(() => {
				ids.value = ids.value.join(',')
				reimApi.deleteAll(ids.value).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
			})
	}
	
	//合计行
	const getSummaries = (param) => {
		const { columns, data } = param
		const sums = []
		columns.forEach((column, index) => {
			if (index === 0) {
				sums[index] = '合计：';
				return;
			}
			const values = data.map((item) => Number(item[column.property]))
			if ((column.property==='totalAmount')) {
				sums[index] = values.reduce((prev, curr) => {
					const value = Number(curr)
					if (!isNaN(value)) {
						return prev + curr
					} else {
						return prev
					}
				}, 0);
				sums[index] += '元'
			}
		});
		return sums;
	}
	
	// 重置搜索
	const originData = () => ({name: "", explain: "",adminId:"",departmentId:"", beginUpdateTime: "", endUpdateTime: ""});
	const resetForm = () => {
		Object.assign(reimQuery, originData());
		loadData()
		selectAllAdmin()
	};
</script>

<template>
	<div class="back">
		<el-card class="">
			<template #header>
				<div class="header">
					<el-button type="danger" :icon="Delete" @click="deleteAll">批量删除</el-button>
				</div>
			</template>
			<!--		搜索框-->
			<el-form :inline="true" class="demo-form-inline">
				<el-form-item label="报销人" v-if="loginUser== 2">
					<el-input v-model="reimQuery.name" placeholder="请输入报销人" clearable style='width:140px'/>
				</el-form-item>
				<el-form-item label="报销原因">
					<el-input v-model="reimQuery.explain" placeholder="请输入报销原因" clearable style='width:160px'/>
				</el-form-item>
				<el-form-item label="报销部门" v-if="loginUser== 1">
					<el-select v-model="reimQuery.departmentId" class="m-2"
							   clearable
							   placeholder="请选择部门"
							   @change='selectByDId'
							   ar='clearSelect' style='width:140px'>
						<el-option
							v-for="item in options"
							:key="item.id"
							:label="item.name"
							:value="item.id"
						/>
					</el-select>
				</el-form-item>
				<el-form-item label="审核人员" v-if="loginUser== 1">
					<el-select v-model="reimQuery.adminId" class="m-2"
							   clearable
							   placeholder="请选择审核人"
							   @change='selectAllAdmin'
							   ar='clearSelect' style='width:130px'>
						<el-option
							v-for="item in aOptions"
							:key="item.id"
							:label="item.name"
							:value="item.id"
						/>
					</el-select>
				</el-form-item>
				<el-form-item label="审批时间">
					<el-date-picker
						v-model="reimQuery.beginUpdateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="开始时间"
						style='width:175px'
					/>
					-
					<el-date-picker
						v-model="reimQuery.endUpdateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="结束时间"
						style='width:175px'
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" :icon="Search" @click="onSearch">搜索</el-button>
					<el-button @click="resetForm" round>重置</el-button>
				</el-form-item>
			</el-form>
			
			<!--		信息表格-->
			<el-table ref="multipleTableRef" :data="reimList" border show-summary stripe
					  style="width: 100%;font-size:16px"
					  height="450px"
					  sum-text="合计："
					  :summary-method="getSummaries"
					  @selection-change="handleSelectionChange" >
				<el-table-column type="selection" :selectable="selectable" min-width="80" align="center" />
				<el-table-column fixed prop="id" label="ID" sortable min-width="70" align="center"  v-if="loginUser == 1" />
				<el-table-column prop="userName" label="报销人" align="center" min-width="110"/>
				<el-table-column prop="departName" label="报销部门" align="center" v-if="loginUser == 1" min-width="120"/>
				<el-table-column prop="totalAmount" label="总金额" align="center" sortable min-width="100"/>
				<el-table-column prop="alipayAccount" label="支付宝账号" align="center" min-width="150" show-overflow-tooltip/>
				<el-table-column prop="explain" label="报销说明" align="center" min-width="180" show-overflow-tooltip/>
				<el-table-column prop="image" label="附件证明" align="center" width="150" show-overflow-tooltip>
					<template #default="scope">
						<img :src="scope.row.image" style="max-height: 40px;max-width: 120px"/>
					</template>
				</el-table-column>
				<el-table-column prop="adminName" label="部门审核人" align="center" v-if="loginUser == 1" min-width="140"/>
				<el-table-column prop="auditExplain" label="审核说明" align="center" min-width="160"/>
				<el-table-column prop="status" label="审核状态" align="center" min-width="160">
					<template #default="scope">
						<div v-if="scope.row.status == 0">未通过</div>
						<div v-if="scope.row.status == 1">部门通过</div>
						<div v-if="scope.row.status == 2">领导通过</div>
						<div v-if="scope.row.status == 3">尚未审批</div>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="申请时间" align="center" min-width="230" sortable/>
				<el-table-column prop="updateTime" label="审核时间" align="center" min-width="230" sortable/>
				<el-table-column fixed="right" label="操作" min-width="120" align="center">
					<template #default="{row}">
						<el-button v-if="row.status == 0 || row.status == 2" type="danger" :icon="Delete" round @click="deleteById(row.id)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				v-model:current-page="reimQuery.page"
				v-model:page-size="reimQuery.limit"
				:page-sizes="[5, 10, 15, 20]"
				:size="size"
				:disabled="disabled"
				:background="background"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
				@change="loadData"
				style="margin-top: 20px; justify-content: flex-end"
			/>
		</el-card>
	</div>

</template>

<style scoped>

</style>
<style>

</style>